home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / dev / misc / AmigaSDLsrc.lha / amisrc / SDL_systimer.c < prev    next >
C/C++ Source or Header  |  2001-05-05  |  5KB  |  272 lines

  1. /* WARNING:  This file was automatically generated!
  2.  * Original: ./src/timer/amigaos/SDL_systimer.c
  3.  */
  4. /*
  5.     SDL - Simple DirectMedia Layer
  6.     Copyright (C) 1997, 1998, 1999, 2000  Sam Lantinga
  7.  
  8.     This library is free software; you can redistribute it and/or
  9.     modify it under the terms of the GNU Library General Public
  10.     License as published by the Free Software Foundation; either
  11.     version 2 of the License, or (at your option) any later version.
  12.  
  13.     This library is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.     Library General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU Library General Public
  19.     License along with this library; if not, write to the Free
  20.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  
  22.     Sam Lantinga
  23.     slouken@devolution.com
  24. */
  25.  
  26. #ifdef SAVE_RCSID
  27. static char rcsid =
  28.  "@(#) $Id: SDL_systimer.c,v 1.1.2.5 2000/05/19 21:59:30 hercules Exp $";
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #include <time.h>
  33. #include <signal.h>
  34. #include <unistd.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #include <exec/types.h>
  38. #ifdef __SASC
  39. #include <proto/dos.h>
  40. #include <clib/graphics_protos.h>
  41. #include <pragmas/graphics.h>
  42. #include <clib/exec_protos.h>
  43. #include <pragmas/exec.h>
  44. #else
  45. #include <inline/dos.h>
  46. #include <inline/exec.h>
  47. #include <inline/graphics.h>
  48. #endif
  49. #include "mydebug.h"
  50.  
  51. extern struct DosLibrary *DOSBase;
  52. extern struct ExecBase *SysBase;
  53. static struct GfxBase *GfxBase;
  54.  
  55. #include "SDL_error.h"
  56. #include "SDL_timer.h"
  57. #include "SDL_timer_c.h"
  58.  
  59. #if defined(DISABLE_THREADS) || defined(FORK_HACK)
  60. #define USE_ITIMER
  61. #endif
  62.  
  63. /* The first ticks value of the application */
  64.  
  65. #ifndef __PPC__
  66. static clock_t start;
  67.  
  68. void SDL_StartTicks(void)
  69. {
  70.     /* Set first ticks value */
  71.     start=clock();
  72. }
  73.  
  74. Uint32 SDL_GetTicks (void)
  75. {
  76.     clock_t ticks;
  77.  
  78.     ticks=clock()-start;
  79.  
  80. #ifdef __SASC
  81. // CLOCKS_PER_SEC == 1000 !
  82.  
  83.     return(ticks);
  84. #else
  85. // CLOCKS_PER_SEC != 1000 !
  86.  
  87.     return ticks*(1000/CLOCKS_PER_SEC);
  88. #endif
  89. }
  90.  
  91. void SDL_Delay (Uint32 ms)
  92. {
  93. // Do a busy wait if time is less than 50ms
  94.  
  95.     if(ms<50)
  96.     {
  97.         clock_t to_wait=clock();
  98.  
  99. #ifndef __SASC
  100.         ms*=(CLOCKS_PER_SEC/1000);
  101. #endif
  102.         to_wait+=ms;
  103.  
  104.         while(clock()<to_wait);
  105.     }
  106.     else
  107.     {
  108.         Delay(ms/20);
  109.     }
  110. }
  111.  
  112. #else
  113.  
  114. ULONG MY_CLOCKS_PER_SEC;
  115.  
  116. void PPC_TimerInit(void);
  117. APTR MyTimer;
  118.  
  119. ULONG start[2];
  120.  
  121. void SDL_StartTicks(void)
  122. {
  123.     /* Set first ticks value */
  124.     if(!MyTimer)
  125.         PPC_TimerInit();
  126.     
  127.     PPCGetTimerObject(MyTimer,PPCTIMERTAG_CURRENTTICKS,start);
  128.     start[1]>>=10;
  129.     start[1]|=((result[0]&0x3ff)<<22);
  130.     start[0]>>=10;
  131. }
  132.  
  133. Uint32 SDL_GetTicks (void)
  134. {
  135.     ULONG result[2];
  136.     PPCGetTimerObject(MyTimer,PPCTIMERTAG_CURRENTTICKS,result);
  137.  
  138. //    PPCAsr64p(result,10);
  139. // Non va, la emulo:
  140.     
  141.     result[1]>>=10;
  142.     result[1]|=((result[0]&0x3ff)<<22);
  143.  
  144. // Non mi interessa piu' result[0]
  145.  
  146.     return result[1]*1000/MY_CLOCKS_PER_SEC;
  147. }
  148.  
  149. void SDL_Delay (Uint32 ms)
  150. {
  151. // Do a busy wait if time is less than 50ms
  152.  
  153.     if(ms<50)
  154.     {
  155.         ULONG to_wait[2],actual[2];
  156.         PPCGetTimerObject(MyTimer,PPCTIMERTAG_CURRENTTICKS,result);
  157.         actual[1]=0;
  158.         to_wait[1]+=ms*1000/MY_CLOCKS_PER_SEC;
  159.  
  160.         while(actual[1]<to_wait[1])
  161.         {
  162.             PPCGetTimerObject(MyTimer,PPCTIMERTAG_CURRENTTICKS,actual);
  163.         }
  164.     }
  165.     else
  166.     {
  167.         Delay(ms/50);
  168.     }
  169. }
  170.  
  171. void PPC_TimerInit(void)
  172. {
  173.     struct TagItem tags[]=
  174.         {
  175.             PPCTIMERTAG_CPU,TRUE,
  176.             TAG_DONE,0
  177.         };
  178.  
  179.  
  180.     if(MyTimer=PPCCreateTimerObject(tags))
  181.     {
  182.         ULONG result[2];
  183.  
  184.         PPCGetTimerObject(MyTimer,PPCTIMERTAG_TICKSPERSEC,result);
  185.         D(bug("Timer inizializzato, TPS: %lu - %lu\n",result[0],result[1]));
  186. //        PPCAsr64p(result,10);
  187.         result[1]>>=10;
  188.         result[1]|=((result[0]&0x3ff)<<22);
  189.         result[0]>>=10;
  190.  
  191.         D(bug("Shiftato TPS: %lu - %lu\n",result[0],result[1]));
  192.         MY_CLOCKS_PER_SEC=result[1];
  193.  
  194.         PPCGetTimerObject(MyTimer,PPCTIMERTAG_CURRENTTICKS,result);
  195.  
  196.         D(bug("Current ticks: %lu - %lu\n",result[0],result[1]));
  197.         result[1]>>=10;
  198.         result[1]|=((result[0]&0x3ff)<<22);
  199.         result[0]>>=10;
  200. //        PPCAsr64p(result,10);
  201.         D(bug("Shiftato: %lu - %lu\n",result[0],result[1]));
  202.     }
  203.     else
  204.     {
  205.         D(bug("Errore nell'inizializzazione del timer!\n"));
  206.     }    
  207. }
  208.  
  209. #endif
  210.  
  211. #include "SDL_thread.h"
  212.  
  213. /* Data to handle a single periodic alarm */
  214. static int timer_alive = 0;
  215. static SDL_Thread *timer_thread = NULL;
  216.  
  217. static int RunTimer(void *unused)
  218. {
  219.     D(bug("SYSTimer: Entering RunTimer loop..."));
  220.  
  221.     if(GfxBase==NULL)
  222.         GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37);
  223.  
  224.     while ( timer_alive ) {
  225.         if ( SDL_timer_running ) {
  226.             SDL_ThreadedTimerCheck();
  227.         }
  228.         if(GfxBase)
  229.             WaitTOF();  // Check the timer every fifth of seconds. Was SDL_Delay(1)->BusyWait!
  230.         else
  231.             Delay(1);
  232.     }
  233.     D(bug("SYSTimer: EXITING RunTimer loop..."));
  234.     return(0);
  235. }
  236.  
  237. /* This is only called if the event thread is not running */
  238. int SDL_SYS_TimerInit(void)
  239. {
  240.     D(bug("Creo il thread per il timer (NOITMER)...\n"));
  241.  
  242.     timer_alive = 1;
  243.     timer_thread = SDL_CreateThread(RunTimer, NULL);
  244.     if ( timer_thread == NULL )
  245.     {
  246.         D(bug("Creazione del thread fallita...\n"));
  247.  
  248.         return(-1);
  249.     }
  250.     return(SDL_SetTimerThreaded(1));
  251. }
  252.  
  253. void SDL_SYS_TimerQuit(void)
  254. {
  255.     timer_alive = 0;
  256.     if ( timer_thread ) {
  257.         SDL_WaitThread(timer_thread, NULL);
  258.         timer_thread = NULL;
  259.     }
  260. }
  261.  
  262. int SDL_SYS_StartTimer(void)
  263. {
  264.     SDL_SetError("Internal logic error: AmigaOS uses threaded timer");
  265.     return(-1);
  266. }
  267.  
  268. void SDL_SYS_StopTimer(void)
  269. {
  270.     return;
  271. }
  272.